// <com.woyouqiu.Copyright>
// --------------------------------------------------------------
// <copyright>上海有求网络科技有限公司 2015</copyright>
// <Solution>Vecen.BangChuang</Solution>
// <Project>Vecen.BangChuang.Core</Project>
// <FileName>CityDomainService.cs</FileName>
// <CreateTime>2017-04-30 10:19</CreateTime>
// <Author>何苗</Author>
// <Email>hemiao@woyouqiu.com</Email>
// <log date="2017-04-30 10:19" version="00001">创建</log>
// --------------------------------------------------------------
// </com.woyouqiu.Copyright>

using System.Threading.Tasks;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Vecen.BangChuang.BangChuang.Areas.DomainDtos;
using Vecen.BangChuang.Helpers;

namespace Vecen.BangChuang.BangChuang.Areas {
    public class CityDomainService: BangChuangDomainServiceBase {
        private readonly IRepository<City, long> _cityRepository;

        public CityDomainService(IRepository<City, long> cityRepository) {
            _cityRepository = cityRepository;
        }

        public async Task<FullCity> GetFullAddressByCityId(long cityId) {
            
            FullCity address = new FullCity();
            return await FillingAddress(address, cityId);
        }

        public async Task<FullCityOutDto> GetFullAddressDtoByCityId(long cityId)
        {
            var result = await GetFullAddressByCityId(cityId);
            return result.MapTo<FullCityOutDto>();
        }

        private async Task<FullCity> FillingAddress(FullCity address, long cityId) {
            var city = await _cityRepository.FirstOrDefaultAsync(cityId);
            if(city != null) {
                if(city.Id.ToString().Length == 6) {
                    address.DistrictId = cityId;
                    address.DistrictName = city.Name;
                } else if(city.Id.ToString().Length == 4) {
                    address.CityId = cityId;
                    address.CityName = city.Name;
                } else if(city.Id.ToString().Length == 2) {
                    address.ProvinceId = cityId;
                    address.ProvinceName = city.Name;
                }

                if(city.ParentId.HasValue) {
                    return await FillingAddress(address, city.ParentId.Value);
                }
            }
            return address;
        }

        public long? GetAreaId(ICityInputDto inputDto)
        {
            return GetAreaId(inputDto.ProvinceId, inputDto.CityId, inputDto.DistrictId);
        }

        public long? GetAreaId(long? provinceId, long? cityId, long? districtId) {
            if(provinceId.HasValue && provinceId.ToString().Length > 2) {
                provinceId = CommonHelper.To<int>(districtId.HasValue ? districtId.Value.ToString().Substring(0, 2)
                    : provinceId.Value.ToString().Substring(0, 2));
            }
            if(cityId.HasValue && cityId.ToString().Length > 4) {
                cityId = CommonHelper.To<int>(districtId.HasValue ? districtId.Value.ToString().Substring(0, 4)
                    : cityId.Value.ToString().Substring(0, 4));
            }
            return districtId ?? cityId ?? provinceId;
        }
        public long GetAreaIdNotNull(ICityInputDto inputDto)
        {
            return GetAreaId(inputDto) ?? 0;
        }
        public long GetAreaIdNotNull(long? provinceId, long? cityId, long? districtId) {
            return GetAreaId(provinceId, cityId, districtId) ?? 0;
        }

        /// <summary>
        /// 根据AreaCode获得地区名称
        /// </summary>
        /// <param name="areaCode"></param>
        /// <returns></returns>
        public async Task<string> GetFullAreaNameByIdAsync(long areaCode) {
            return (await _cityRepository.FirstOrDefaultAsync(areaCode))?.FullName;
        }
    }
}